package com.huibao.hulibao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.example.zhouwei.library.CustomPopWindow;
import com.huibao.hulibao.R;
import com.huibao.hulibao.activity.MainActivity;
import com.huibao.hulibao.entity.CheckEntity;
import com.huibao.hulibao.fragment.LocationFragmentView;
import com.huibao.hulibao.utils.T;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lizhaoyong on 2018/5/7.
 */
public class CheckLvAdapter extends BaseAdapter {
    private List<CheckEntity.CheckDetailData> mDatas;
    private Context mContext;

    private LayoutInflater mInflater;
    private LocationFragmentView locationFragmentView;

    public CheckLvAdapter(List<CheckEntity.CheckDetailData> datas, Context context, LocationFragmentView locationFragmentView) {
        this.mDatas = datas;
        this.mContext = context;
        this.locationFragmentView = locationFragmentView;
        mInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        final CheckEntity.CheckDetailData cd = mDatas.get(position);


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_check, parent, false);
            holder = new ViewHolder(convertView);
            // holder.mTextView = (TextView)convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_lv_checke_title.setText(cd.AddTime);

        if (cd.Location.equals("")) {
            holder.tv_address.setVisibility(View.GONE);
        } else {
            holder.tv_address.setText(cd.Location);
            holder.tv_address.setVisibility(View.VISIBLE);
        }

        holder.tv_footer.setText("备注:" + cd.CheckLabelName);

        // LogUtils.d("img = " + cd.Images);

        if (cd.Images.equals("")) {
            holder.ll_lv_check_body.setVisibility(View.GONE);
        } else {
            holder.ll_lv_check_body.setVisibility(View.VISIBLE);
            if (cd.Images.contains(";")) {
                final String[] arr = cd.Images.split(";");
                if (arr.length == 2) {
                    Glide.with(mContext).load(arr[0].replace("\\", "")).into(holder.iv_lv_check_body_one);
                    holder.iv_lv_check_body_one.setVisibility(View.VISIBLE);
                    Glide.with(mContext).load(arr[1].replace("\\", "")).into(holder.iv_lv_check_body_two);
                    holder.iv_lv_check_body_two.setVisibility(View.VISIBLE);
                    holder.iv_lv_check_body_three.setVisibility(View.INVISIBLE);

                    holder.iv_lv_check_body_one.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            locationFragmentView.enterBigPicActivity(arr[0].replace("\\", ""));
                        }
                    });

                    holder.iv_lv_check_body_two.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            locationFragmentView.enterBigPicActivity(arr[1].replace("\\", ""));
                        }
                    });


                } else if (arr.length == 3) {
                    Glide.with(mContext).load(arr[0].replace("\\", "")).into(holder.iv_lv_check_body_one);
                    holder.iv_lv_check_body_one.setVisibility(View.VISIBLE);
                    Glide.with(mContext).load(arr[1].replace("\\", "")).into(holder.iv_lv_check_body_two);
                    holder.iv_lv_check_body_two.setVisibility(View.VISIBLE);
                    Glide.with(mContext).load(arr[2].replace("\\", "")).into(holder.iv_lv_check_body_three);
                    holder.iv_lv_check_body_three.setVisibility(View.VISIBLE);

                    holder.iv_lv_check_body_one.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            locationFragmentView.enterBigPicActivity(arr[0].replace("\\", ""));
                        }
                    });

                    holder.iv_lv_check_body_two.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            locationFragmentView.enterBigPicActivity(arr[1].replace("\\", ""));

                        }
                    });

                    holder.iv_lv_check_body_three.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            locationFragmentView.enterBigPicActivity(arr[2].replace("\\", ""));
                        }
                    });


                }
            } else {
                Glide.with(mContext).load(cd.Images.replace("\\", "")).into(holder.iv_lv_check_body_one);
                holder.iv_lv_check_body_one.setVisibility(View.VISIBLE);
                holder.iv_lv_check_body_two.setVisibility(View.INVISIBLE);
                holder.iv_lv_check_body_three.setVisibility(View.INVISIBLE);

                holder.iv_lv_check_body_one.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        // locationFragmentView.showPopWindow(cd.Images.replace("\\", ""));
                        locationFragmentView.enterBigPicActivity(cd.Images.replace("\\", ""));
                        // LogUtils.d("url = "+ cd.Images.replace("\\", ""));

                    }
                });
            }
        }

        // holder.mTextView.setText(mDatas.get(position));

        return convertView;
    }


    static class ViewHolder {

        @BindView(R.id.iv_lv_check_icon)
        ImageView iv_lv_check_icon;
        @BindView(R.id.tv_lv_checke_title)
        TextView tv_lv_checke_title;
        @BindView(R.id.iv_lv_check_body_one)
        ImageView iv_lv_check_body_one;
        @BindView(R.id.iv_lv_check_body_two)
        ImageView iv_lv_check_body_two;
        @BindView(R.id.iv_lv_check_body_three)
        ImageView iv_lv_check_body_three;
        @BindView(R.id.tv_footer)
        TextView tv_footer;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.ll_lv_check_body)
        LinearLayout ll_lv_check_body;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
